﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace GM.Forms
{
    public partial class MainForm : Form
    {
        private readonly AssetForm m_assetForm;
        private readonly LoginForm m_loginForm;
        private readonly MaterialForm m_materialForm;
        private readonly RepairForm m_repairForm;

        public MainForm()
        {
            InitializeComponent();

            m_assetForm = new AssetForm {Visible = false};

            m_loginForm = new LoginForm { Visible = false };
            m_loginForm.LoginRequested += LoginForm_LoginRequested;
            m_loginForm.LoginCancelled += LoginForm_LoginCancelled;

            m_materialForm = new MaterialForm {Visible = false};

            m_repairForm = new RepairForm {Visible = false};

            lblVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void LoginForm_LoginCancelled()
        {
            Close();
        }

        private void LoginForm_LoginRequested(bool obj)
        {
            if(obj)
            {
                m_loginForm.Hide();
                Show();
                BringToFront();
            }
            else
            {
                Hide();
                m_loginForm.ShowDialog();
            }
        }

        private void gbtnAssets_Click(object sender, EventArgs e)
        {
            if (m_assetForm.Visible)
                m_assetForm.BringToFront();
            else
                m_assetForm.Show();
        }

        private void gbtnMaterials_Click(object sender, EventArgs e)
        {
            if (m_materialForm.Visible)
                m_materialForm.BringToFront();
            else
                m_materialForm.Show();
        }

        private void gbtnRepairs_Click(object sender, EventArgs e)
        {
            if (m_repairForm.Visible)
                m_repairForm.BringToFront();
            else
                m_repairForm.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Hide();
            m_loginForm.ShowDialog();
        }
    }
}
